package com.zcx.liu_zi_qi

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF

/**
 *棋盘类
 */
class ChessBoard(val canvas: Canvas) {
    val width = canvas.height - 200//棋盘宽度
    val height = canvas.height - 200//棋盘高度
    val block = width / 3
    val rect: RectF = RectF(100f, 100f, (width + 100).toFloat(), (height + 100).toFloat())

    /**
     *内部棋子类
     * i:横坐标(0,1,2,3)
     * j:纵坐标(0,1,2,3)
     */
    inner class ChessMan(var i:Int=0,var j:Int=0,var type:ChessManType=ChessManType.WHITE):Cloneable{
        var cx=this@ChessBoard.rect.left+j*block//棋子中心x坐标
        var cy=this@ChessBoard.rect.top+i*block//棋子中心y坐标
        var isSelect=false//标记棋子是否被选中
        var callMoveDirection= arrayListOf<ChessManDirection>()//棋子可移动方向

        override fun toString():String{
            return "{i:${this.i},j:${this.j},cx:${this.cx},cy:${this.cy},type:${this.type},isSelect:${this.isSelect}}"
        }

        fun copy(): ChessMan {
            val temp=ChessMan(i,j)
            temp.cx=cx
            temp.cy=cy
            temp.type=type
            temp.isSelect=isSelect
            return temp
        }
        /**
         * 绘制棋子
         */
        fun draw(r:Float=50f){
            val paint=Paint()
            paint.style=Paint.Style.FILL_AND_STROKE
            paint.strokeWidth=4f
            paint.color=when(type){
                ChessManType.WHITE -> Color.WHITE
                else -> Color.BLACK
            }
            canvas.drawCircle(cx,cy,r,paint)

            paint.color=Color.BLACK
            paint.style=Paint.Style.STROKE
            canvas.drawCircle(cx,cy,r,paint)
            if(isSelect){
                paint.color=Color.YELLOW
                paint.strokeWidth=6f
                paint.strokeCap=Paint.Cap.SQUARE//设置笔刷图形样式
                paint.strokeMiter=10f//设置笔画倾斜度
                canvas.drawRect(
                    RectF((cx-60).toFloat(),(cy-60).toFloat(),(cx+60).toFloat(),(cy+60).toFloat()),
                    paint
                )
            }
        }

        /**
         * 移动棋子
         */
        fun move(i:Int,j:Int):Boolean{
            if((Math.abs(this.i-i)<1&&this.i==i)
                ||(Math.abs(this.j-j)<1&&this.j==j)){
                if(Map.history.size>5){//历史记录大于5,清空
                    Map.history.removeAll(Map.history)
                }
                Map.history.add(Pair(Map.copyList(),Map.copyMap()))
                this.cx=this@ChessBoard.rect.left+j*block
                this.cy=this@ChessBoard.rect.top+i*block
                Map.map[this.i][this.j]=0
                this.i=i
                this.j=j
                Map.map[i][j]=when(type){
                    ChessManType.BLACK ->2
                    else -> 1
                }
                return true
                //Map.printMap()
                //Map.printList()
            }
            return false
        }

        /**
         * 检查
         */
        fun check() {
            val tp=when(type){ChessManType.WHITE ->1 else ->2}
            val _tp=when(type){ChessManType.WHITE ->2 else ->1}
            when(i){
                0 ->{
                    if(Map.map[1][j]==tp&&Map.map[2][j]==_tp&&Map.map[3][j]==0){
                        Map.map[2][j]=0
                        Map.list.filter { it.i==2&&it.j==this.j }[0].destroy()
                    }
                }
                1 ->{
                    if(Map.map[0][j]==tp&&Map.map[2][j]==_tp&&Map.map[3][j]==0){
                        Map.map[2][j]=0
                        Map.list.filter { it.i==2&&it.j==this.j }[0].destroy()
                    }
                    if(Map.map[0][j]==0&&Map.map[2][j]==tp&&Map.map[3][j]==_tp){
                        Map.map[3][j]=0
                        Map.list.filter { it.i==3&&it.j==this.j }[0].destroy()
                    }
                    if(Map.map[0][j]==_tp&&Map.map[2][j]==tp&&Map.map[3][j]==0){
                        Map.map[0][j]=0
                        Map.list.filter { it.i==0&&it.j==this.j }[0].destroy()
                    }
                }
                2 ->{
                    if(Map.map[0][j]==0&&Map.map[1][j]==_tp&&Map.map[3][j]==tp){
                        Map.map[1][j]=0
                        Map.list.filter { it.i==1&&it.j==this.j }[0].destroy()
                    }
                    if(Map.map[0][j]==_tp&&Map.map[1][j]==tp&&Map.map[3][j]==0){
                        Map.map[0][j]=0
                        Map.list.filter { it.i==0&&it.j==this.j }[0].destroy()
                    }
                    if(Map.map[0][j]==0&&Map.map[1][j]==tp&&Map.map[3][j]==_tp){
                        Map.map[3][j]=0
                        Map.list.filter { it.i==3&&it.j==this.j }[0].destroy()
                    }
                }
                3 ->{
                    if(Map.map[0][j]==0&&Map.map[1][j]==_tp&&Map.map[2][j]==tp){
                        Map.map[1][j]=0
                        Map.list.filter { it.i==1&&it.j==this.j }[0].destroy()
                    }
                }
            }
            when(j){
                0 ->{
                    if(Map.map[i][1]==tp&&Map.map[i][2]==_tp&&Map.map[i][3]==0){
                        Map.map[i][2]=0
                        Map.list.filter { it.j==2&&it.i==this.i }[0].destroy()
                    }
                }
                1 ->{
                    if(Map.map[i][0]==tp&&Map.map[i][2]==_tp&&Map.map[i][3]==0){
                        Map.map[i][2]=0
                        Map.list.filter { it.j==2&&it.i==this.i }[0].destroy()
                    }
                    if(Map.map[i][0]==0&&Map.map[i][2]==tp&&Map.map[i][3]==_tp){
                        Map.map[i][3]=0
                        Map.list.filter { it.j==3&&it.i==this.i }[0].destroy()
                    }
                    if(Map.map[i][0]==_tp&&Map.map[i][2]==tp&&Map.map[i][3]==0){
                        Map.map[i][0]=0
                        Map.list.filter { it.j==0&&it.i==this.i }[0].destroy()
                    }
                }
                2 ->{
                    if(Map.map[i][0]==0&&Map.map[i][1]==_tp&&Map.map[i][3]==tp){
                        Map.map[i][1]=0
                        Map.list.filter { it.j==1&&it.i==this.i }[0].destroy()
                    }
                    if(Map.map[i][0]==_tp&&Map.map[i][1]==tp&&Map.map[i][3]==0){
                        Map.map[i][0]=0
                        Map.list.filter { it.j==0&&it.i==this.i }[0].destroy()
                    }
                    if(Map.map[i][0]==0&&Map.map[i][1]==tp&&Map.map[i][3]==_tp){
                        Map.map[i][3]=0
                        Map.list.filter { it.j==3&&it.i==this.i }[0].destroy()
                    }
                }
                3 ->{
                    if(Map.map[i][0]==0&&Map.map[i][1]==_tp&&Map.map[i][2]==tp){
                        Map.map[i][1]=0
                        Map.list.filter { it.j==1&&it.i==this.i }[0].destroy()
                    }
                }
            }

        }

        /**
         * 销毁
         */
        fun destroy(){
            Map.list.remove(this)

        }
    }

    /**
     *绘制棋盘
     */
    fun drawChessBoard(){
        val paint=Paint()
        paint.color=Color.BLUE
        paint.style=Paint.Style.STROKE
        paint.strokeWidth=8f
        for (i in 0..2){
            canvas.drawRect(
                RectF(rect.left,rect.top+i*block.toFloat(),rect.right,rect.top+(i+1)*block.toFloat()),
                paint
            )
            canvas.drawRect(
                RectF(rect.left+i*block.toFloat(),rect.top,rect.left+(i+1)*block.toFloat(),rect.bottom),
                paint
            )
        }
    }

    /**
     * 更新棋盘
     */
     fun update(){
        this.drawChessBoard()
        synchronized(Map){
            if(Map.list.isEmpty()){
                Map.list.removeAll(Map.list)
                for (i in 0..3){
                    for (j in 0..3){
                        if(Map.map[i][j]==1){
                            Map.list.add(ChessMan(i,j,ChessManType.WHITE))
                        }
                        if(Map.map[i][j]==2){
                            Map.list.add(ChessMan(i,j,ChessManType.BLACK))
                        }
                    }
                }
            }


            Map.list.forEach {
                //println(it.toString())
                it.draw()
            }
        }

    }

    /**
     * 将x,y转换成i,j
     */
    fun xy2ij(pair: Pair<Float,Float>): Pair<Int, Int>? {
        val x=pair.first
        val y=pair.second
        if(x>rect.right+block/2){
            return null
        }
        val j=((x-rect.left+block/2)/block).toInt()
        val i=((y-rect.top+block/2)/block).toInt()
        return Pair(i,j)
    }
}